package com.wondertek.liveroom;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.jasontools.data.ChatMessage;
import com.jasontools.utils.ChatEmojiConvertUtil;
import com.jasontools.utils.NicknameUtil;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class FragmentLiveChat extends Fragment implements ShowMessage{
	private View view = null;
//	private LinearLayout llChatMessageContainer = null;
//	private ScrollView svChatMessageContainer = null;

//	private List<SpannableString> spanStrList = new ArrayList<SpannableString>();
	private List<ChatMessage> chatMsgList = new ArrayList<ChatMessage>();
	
	private ListView msgListView = null;
	private ChatMessageAdapter adapter = null;
	
	
	private Handler myHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch(msg.what) {
			case LiveActivity.MSG_SHOW_CHAT:
				Bundle bundle = msg.getData();				
				addChatMessageView(bundle.getString("from"), bundle.getString("content"));
				break;
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_room_chat, null);
		initCtrl();
		return view;
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		adapter.notifyDataSetChanged();
	}
	
	private void initCtrl() {
	//	this.llChatMessageContainer = (LinearLayout) view.findViewById(R.id.ll_chat_msg_container);
	//	this.svChatMessageContainer = (ScrollView) view.findViewById(R.id.sv_chat_message_container);		
		this.msgListView = (ListView) view.findViewById(R.id.room_message_list);
		adapter = new ChatMessageAdapter(getActivity(), chatMsgList);
		msgListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		if (msgListView != null) {
			msgListView.setSelection(chatMsgList.size() - 1);
		}
		((LiveActivity) this.getActivity()).setShowMessage(this);
	}
	
	private void addChatMessageView(String from, String message) {
		//String strMsg = from + ": " + content;
//		TextView tvMsg = new TextView(this.getActivity());
//		LinearLayout.LayoutParams  lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
//				LayoutParams.WRAP_CONTENT);
//		lp.leftMargin = 20;
//		lp.topMargin = 10;
		//String strText = String.format(this.getActivity().getResources().getString(R.string.style_chat_message), from, message);
		//tvMsg.setText(Html.fromHtml(strText));
		String bareFrom = NicknameUtil.getNicknameFromFullJid(from);
		String strText = bareFrom + ": " + message;
		System.out.println(strText);
		//convert [em001] to image
		
		SpannableString spannableString = ChatEmojiConvertUtil.getInstance().getExpressionString(getActivity(), message);		
  //    tvMsg.setText(spannableString);
  //    spanStrList.add(spannableString);
        ChatMessage chatMsg = new ChatMessage();       
        chatMsg.setSpanChatContent(spannableString);
        chatMsg.setChatTime(getTimeNow());
        chatMsgList.add(chatMsg);
        adapter.notifyDataSetChanged();
      //msgListView.setSelection(spanStrList.size() - 1);
        
	  //this.llChatMessageContainer.addView(tvMsg);
		
		
		//scoll the view to the end
//		myHandler.post(new Runnable() {
//			@Override
//			public void run() {
//				svChatMessageContainer.fullScroll(ScrollView.FOCUS_DOWN);
        		
//			}
//		});
	}

	@Override
	public void show(String from, String content) {
		//addChatMessageView(from, content);
		Message msg = myHandler.obtainMessage();
		msg.what = LiveActivity.MSG_SHOW_CHAT;
		Bundle bundle = new Bundle();
		bundle.putString("from", from);
		bundle.putString("content", content);
		msg.setData(bundle);
		myHandler.sendMessage(msg);
	}
	
	private String getTimeNow() {
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");  
		String time = sdf.format(new java.util.Date());  
		return time;
	}
}
